package com.example.bootdemo.controller;

import com.example.bootdemo.pojo.Greeting;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";

    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
       return new Greeting(counter.incrementAndGet(),String.format(template,name));
    }

    @GetMapping("/greeting/{name}")
    public Greeting pathGreeting(@PathVariable(value="name") String name){
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

    @PostMapping("/greeting")
    public String createGreeting(@RequestBody Greeting greeting){
        System.out.println(greeting.toString());
        return "success";
    }

}
